package com.sky.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.LinkedList;

public class FileClassification {
    final private static String targetPath = "D:\\Documents\\spring_pdf\\";

    static void method(File dir) throws Exception {
        File[] fs = dir.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].isDirectory()) {
                method(fs[i]);
            } else {
                String suffix = fs[i].getName().substring(fs[i].getName().lastIndexOf(".") + 1, fs[i].getName().length());//后缀名
                if (suffix.equals("pdf")) {//pdf文件
                    File targetFile = new File(targetPath + fs[i].getName());
                    if (!targetFile.exists())
                        targetFile.createNewFile();
                    FileOutputStream fos = new FileOutputStream(targetFile);
                    FileInputStream fis = new FileInputStream(fs[i]);
                    byte[] buf = new byte[1024];
                    int len = -1;
                    while ((len = fis.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }
                    fis.close();
                    fos.flush();
                    fos.close();
                }
            }
        }

    }

    public static void main(String[] args) throws Exception {

        String path = "D:\\Data\\BaiduNetdiskDownload\\资料-SS M框架Spring+SpringMVC+MyBatis全覆盖";
        method(new File(path));

    }

}
